package com.ankiang.treeprint.fragment.components;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SnapHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.ankiang.treeprint.R;
import com.ankiang.treeprint.adaptor.TRRecyclerViewAdapter;
import com.ankiang.treeprint.base.BaseFragment;
import com.ankiang.treeprint.lib.Group;
import com.ankiang.treeprint.lib.annotation.Widget;
import com.ankiang.treeprint.manager.QDDataManager;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author cginechen
 * @date 2017-09-13
 */

@Widget(group = Group.Home, widgetClass = TRGalleryFragment.class, iconRes = R.drawable.ic_icon_gallery)
public class TRGalleryFragment extends BaseFragment {
    @BindView(R.id.topbar) QMUITopBar mTopBar;
//    @BindView(R.id.pager) QMUIViewPager mViewPager;


    RecyclerView mRecyclerView;
    LinearLayoutManager mPagerLayoutManager;
    TRRecyclerViewAdapter mRecyclerViewAdapter;
    SnapHelper mSnapHelper;

    @BindView(R.id.pagerWrap) ViewGroup mPagerWrap;

//    private List<Integer> mItems = new ArrayList<>();


    @Override
    protected View onCreateView() {
        FrameLayout layout = (FrameLayout) LayoutInflater.from(getActivity()).inflate(R.layout.tr_fragment_gallery, null);
        ButterKnife.bind(this, layout);



        initData();
        initTopBar();
//        initPagers();
        return layout;
    }

    private void initData() {



        mRecyclerView = new RecyclerView(getContext());
        mPagerLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(mPagerLayoutManager);
        mRecyclerViewAdapter = new TRRecyclerViewAdapter();
//        mRecyclerViewAdapter.setItemCount(mItems.size());

        mRecyclerView.setAdapter(mRecyclerViewAdapter);
        mPagerWrap.addView(mRecyclerView);
        // PagerSnapHelper每次只能滚动一个item;用LinearSnapHelper则可以一次滚动多个，并最终保证定位
        // mSnapHelper = new LinearSnapHelper();
        mSnapHelper = new PagerSnapHelper();
        mSnapHelper.attachToRecyclerView(mRecyclerView);

        mPagerLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);




        R.drawable a = new R.drawable();


        Field[] tmpList = a.getClass().getDeclaredFields();
        for (int i = 0; i < tmpList.length; i++) {
//            tmpList[i].get();
            mRecyclerViewAdapter.addItem(i, R.drawable.abc_ab_share_pack_mtrl_alpha + i);
//            mItems.add(R.drawable.abc_ab_share_pack_mtrl_alpha + i);
        }
    }

    private void initTopBar() {
        mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popBackStack();
            }
        });
        mTopBar.setTitle(QDDataManager.getInstance(getContext()).getDescription(this.getClass()).getName());
    }

}
